﻿using LayuiDemo.Models;
using LayuiDemo.ViewModels.BlogVMs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace LayuiDemo.Controllers
{

	[ActionDescription("Blog")]
	public partial class BlogController : BaseController
	{
		public IActionResult GetTrees()
		{
			var list = new List<TreeSelectListItem>();
			var child = new List<TreeSelectListItem>();
			child.Add(new TreeSelectListItem
			{
				Text = "这是一个超长的文本这是一个超长的文本",
				Tag = "222",
				Url = "/",
				Value = "1",

			});
			child.Add(new TreeSelectListItem
			{
				Text = "222",
				Tag = "222",
				Url = "/",
				Value = "3245"
			});

			for (int i = 0; i < 2; i++)
			{
				list.Add(new TreeSelectListItem
				{
					Text = i.ToString(),
					Tag = "222",
					Url = "/",
					Value = i,
					Children = child
				});
			}

			return JsonMore(list);
		}
		#region TestIds

		public ActionResult TestIds()
		{
			var vm = Wtm.CreateVM<BlogVM>();
			return PartialView(vm);
		}

		[HttpPost]

		public ActionResult TestIds(BlogVM vm)
		{
			if (!ModelState.IsValid)
			{
				return PartialView(vm);
			}
			else
			{
				vm.DoAdd();
				if (!ModelState.IsValid)
				{
					vm.DoReInit();
					return PartialView(vm);
				}
				else
				{
					return FFResult().CloseDialog().RefreshGrid();
				}
			}
		}
		#endregion

		#region View

		public ActionResult SomeView()
		{
			return PartialView();
		}
		#endregion

		#region Search
		[ActionDescription("Sys.Search")]
		public ActionResult Index()
		{
			dynamic a = new { Name = "lisan" };
			var vm = Wtm.CreateVM<BlogListVM>();

			return PartialView(vm);
		}

		[ActionDescription("Sys.Search")]
		[HttpPost]
		public string Search(BlogSearcher searcher)
		{

			var vm = Wtm.CreateVM<BlogListVM>(passInit: true);
			if (ModelState.IsValid)
			{
				vm.Searcher = searcher;
				return vm.GetJson(false);
			}
			else
			{
				return vm.GetError();
			}
		}

		#endregion

		#region Create
		[ActionDescription("Sys.Create")]
		public ActionResult Create()
		{
			var vm = Wtm.CreateVM<BlogVM>();
			return PartialView(vm);
		}

		[HttpPost]
		[ActionDescription("Sys.Create")]
		public ActionResult Create(BlogVM vm)
		{
			Wtm.MSD.AddModelError("Entity.Name", "test");
			Wtm.MSD.AddModelError("", "test22");
			return PartialView(vm);
			//if (!ModelState.IsValid)
			//{
			//	return PartialView(vm);
			//}
			//else
			//{
			//	vm.DoAdd();
			//	if (!ModelState.IsValid)
			//	{
			//		vm.DoReInit();
			//		return PartialView(vm);
			//	}
			//	else
			//	{
			//		return FFResult().CloseDialog().RefreshGrid();
			//	}
			//}
		}
		#endregion

		#region Edit
		[ActionDescription("Sys.Edit")]
		public ActionResult Edit(string id)
		{
			var vm = Wtm.CreateVM<BlogVM>(id);
			return PartialView(vm);
		}

		[ActionDescription("Sys.Edit")]
		[HttpPost]
		[ValidateFormItemOnly]
		public ActionResult Edit(BlogVM vm)
		{
			if (!ModelState.IsValid)
			{
				return PartialView(vm);
			}
			else
			{
				vm.DoEdit();
				if (!ModelState.IsValid)
				{
					vm.DoReInit();
					return PartialView(vm);
				}
				else
				{
					return FFResult().CloseDialog().RefreshGridRow(vm.Entity.ID);
				}
			}
		}
		#endregion

		#region Delete
		[ActionDescription("Sys.Delete")]
		public ActionResult Delete(string id)
		{
			var vm = Wtm.CreateVM<BlogVM>(id);
			return PartialView(vm);
		}

		[ActionDescription("Sys.Delete")]
		[HttpPost]
		public ActionResult Delete(string id, IFormCollection nouse)
		{
			var vm = Wtm.CreateVM<BlogVM>(id);
			vm.DoDelete();
			if (!ModelState.IsValid)
			{
				return PartialView(vm);
			}
			else
			{
				return FFResult().CloseDialog().RefreshGrid();
			}
		}
		#endregion

		#region Details
		[ActionDescription("Sys.Details")]
		public ActionResult Details(string id)
		{
			var vm = Wtm.CreateVM<BlogVM>(id);
			return PartialView(vm);
		}
		#endregion

		#region BatchEdit
		[HttpPost]
		[ActionDescription("Sys.BatchEdit")]
		public ActionResult BatchEdit(string[] IDs)
		{
			var vm = Wtm.CreateVM<BlogBatchVM>(Ids: IDs);
			return PartialView(vm);
		}

		[HttpPost]
		[ActionDescription("Sys.BatchEdit")]
		public ActionResult DoBatchEdit(BlogBatchVM vm, IFormCollection nouse)
		{
			if (!ModelState.IsValid || !vm.DoBatchEdit())
			{
				return PartialView("BatchEdit", vm);
			}
			else
			{
				return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.BatchEditSuccess", vm.Ids.Length]);
			}
		}
		#endregion

		#region BatchDelete
		[HttpPost]
		[ActionDescription("Sys.BatchDelete")]
		public ActionResult BatchDelete(string[] IDs)
		{
			var vm = Wtm.CreateVM<BlogBatchVM>(Ids: IDs);
			return PartialView(vm);
		}

		[HttpPost]
		[ActionDescription("Sys.BatchDelete")]
		public ActionResult DoBatchDelete(BlogBatchVM vm, IFormCollection nouse)
		{
			if (!ModelState.IsValid || !vm.DoBatchDelete())
			{
				return PartialView("BatchDelete", vm);
			}
			else
			{
				return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.BatchDeleteSuccess", vm.Ids.Length]);
			}
		}
		#endregion

		#region Import
		[ActionDescription("Sys.Import")]
		public ActionResult Import()
		{
			var vm = Wtm.CreateVM<BlogImportVM>();
			return PartialView(vm);
		}

		[HttpPost]
		[ActionDescription("Sys.Import")]
		public ActionResult Import(BlogImportVM vm, IFormCollection nouse)
		{
			if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
			{
				return PartialView(vm);
			}
			else
			{
				return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.ImportSuccess", vm.EntityList.Count.ToString()]);
			}
		}
		#endregion

		[ActionDescription("Sys.Export")]
		[HttpPost]
		public IActionResult ExportExcel(BlogListVM vm)
		{
			return vm.GetExportData();
		}

		[HttpGet]
		public IActionResult BlogFilter(string keyworkds)
		{
			var data = DC.Set<Blog>().CheckContain(keyworkds, x => x.Name);
			return JsonMore(data);
		}

	}
}
